package chinatelecom.feilong.framework.web.sys.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import chinatelecom.feilong.framework.common.controller.BaseController;
import chinatelecom.feilong.framework.web.sys.entity.SysUser;
import chinatelecom.feilong.framework.web.sys.entity.SysUserRoleRelation;
import chinatelecom.feilong.framework.web.sys.service.IUserService;

/**
 * UserController 系统用户模块控制器
 * JRed(breavecatking@gmail.com)
 * 2016/6/23 15:23
 **/
@Controller
@RequestMapping("/sys/user/")
public class UserController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    private IUserService userService;


    /**
     *
     * @Title: userList
     * @Description:用户列表
     * @author JRed bravecatking@gmail.com
     * @param @param request
     * @param @param model
     * @param @return
     * @return String
     * @throws
     */
    @RequiresPermissions("sys:user:list")
    @RequestMapping(value = "user_list")
    public String userList(final HttpServletRequest request, final Model model) {
        return "modules/sys/user/user_list";
    }

    /**
     * 异步获取用户数据
     * @param request
     * @param rows
     * @param page
     * @return
     */
    @RequiresPermissions("sys:user:list")
    @RequestMapping(value = "ajax_user_data")
    @ResponseBody
    public Map ajaxUserData(final HttpServletRequest request, final Integer rows,
            final Integer page, final String sortName, final String sortValue) {
        Integer pageSize = rows == null ? 10 : rows;
        Integer curPage = page == null ? 0 : page - 1;
        Map<String, Object> searchParams = new HashMap<String, Object>();
        String username = request.getParameter("username");
        if (StringUtils.isNotBlank(username)) {
            searchParams.put("LIKE_username", username);
        }
        Pageable pageable = new PageRequest(curPage, pageSize);
        Page<SysUser> userPage =
                userService.findUserPageByParams(searchParams, pageable, sortName, sortValue);
        Map data = new HashMap();
        data.put("total", userPage.getTotalElements());
        data.put("rows", userPage.getContent());
        return data;
    }


    private String[] getSelectedRoleIds(final List<SysUserRoleRelation> list) {
        String ids = "";
        String names = "";
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i < list.size() - 1) {
                    ids += list.get(i).getSysRoleByRoleId().getId() + ",";
                    names += list.get(i).getSysRoleByRoleId().getRoleName() + ",";
                } else {
                    ids += list.get(i).getSysRoleByRoleId().getId();
                    names += list.get(i).getSysRoleByRoleId().getRoleName();
                }
            }
        }
        return new String[] {ids, names};
    }


    /**
     *
     * @Title: userAdd
     * @Description: 用户添加页面
     * @author JRed bravecatking@gmail.com
     * @param @param id
     * @param @param model
     * @param @return
     * @return String
     * @throws
     */
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "user_add")
    public String userAdd(@RequestParam(required = false) final String id, final Model model) {
        if (StringUtils.isNotEmpty(id)) {
            SysUser user = userService.getById(Integer.valueOf(id));
            model.addAttribute("user", user);

            String[] sels =
                    getSelectedRoleIds(userService.getUserRoleRelationByUserId(user.getId()));
            model.addAttribute("selRoleIds", sels[0]);
            model.addAttribute("selRoleNames", sels[1]);
        }
        return "modules/sys/user/user_add";
    }

    @RequiresPermissions("sys:user:save")
    @RequestMapping(value = "user_save")
    public String userSave(final SysUser user, final Model model, final HttpServletRequest request,
            final RedirectAttributes redirectAttr, final String roleIds, final String isBack) {
        int userPkId = user.getId();
        if (!beanValidator(model, user)) {
            model.addAttribute("message", "必填项不可为空！");
            if (user.getId() == 0) {
                return userAdd(null, model);
            } else {
                return userAdd(userPkId + "", model);
            }
        }
        try {
            Map params = new HashMap();
            if (StringUtils.isNotBlank(roleIds)) {
                params.put("roleIds", roleIds.split(","));
            }
            userService.saveUser(user, params);
            //注释理想代码
            //            redirectAttr.addFlashAttribute("message", "保存用户[" + user.getUsername() + "]成功!");
            //增加提交成功页面，设置页面展示值
            request.setAttribute("message", "保存用户[" + user.getUsername() + "]成功!");
            return "commons/successToSelf";

        } catch (Exception e) {
            //            redirectAttr.addFlashAttribute("message", "保存用户[" + user.getUsername() + "]失败!");
            request.setAttribute("message", "保存用户[" + user.getUsername() + "]失败!");
            return "commons/error";
        }

    }


    @RequiresPermissions("sys:user:delete")
    @RequestMapping(value = "user_delete")
    @ResponseBody
    public Integer userDel(@RequestParam final String idStr, final RedirectAttributes redirectAttr) {
        Integer result = 0;
        try {
            String[] ids = idStr.split(",");
            userService.delUserByIds(ids);
        } catch (Exception e) {
            logger.error("删除用户失败", e);
            result = 1;
        }
        return result;
    }

    /**
     * 分配角色页面
     * @param id
     * @param model
     * @author cuiyg
     * @return
     */
    @RequiresPermissions("sys:user:allocation")
    @RequestMapping(value = "user_allocation")
    public String userAllocation(@RequestParam(required = false) final String id, @RequestParam(
            required = false) final String username, final Model model) {
        if (StringUtils.isNotEmpty(id)) {
            //获取用户
            SysUser user = new SysUser();
            user.setId(Integer.parseInt(id));
            user.setUsername(username);
            model.addAttribute("user", user);
            //获取用户角色列表
            String[] sels =
                    getSelectedRoleIds(userService.getUserRoleRelationByUserId(Integer.valueOf(id)));
            model.addAttribute("selRoleIds", sels[0]);
            model.addAttribute("selRoleNames", sels[1]);
        }
        return "modules/sys/user/user_authority";
    }

    /**
     * 用户分配权限
     * @author cuiyg
     * @param user
     * @param model
     * @param redirectAttr
     * @param roleIds
     * @param isBack
     * @return
     */
    @RequiresPermissions("sys:user:userrolesave")
    @RequestMapping(value = "user_role_save")
    public String userRoleSave(final SysUser user, final Model model,
            final RedirectAttributes redirectAttr, final String roleIds, final String isBack) {

        try {
            int userPkId = user.getId();
            Map params = new HashMap();
            if (userPkId > 0) {
                if (StringUtils.isNotBlank(roleIds)) {
                    params.put("roleIds", roleIds.split(","));
                }
            }
            userService.saveUserRole(user, params);
            redirectAttr.addFlashAttribute("message", "用户[" + user.getUsername() + "]分配角色成功!");
            redirectAttr.addAttribute("id", userPkId);
            redirectAttr.addAttribute("username", user.getUsername());
        } catch (Exception e) {
            redirectAttr.addFlashAttribute("message", "用户[" + user.getUsername() + "]分配角色失败!");
            redirectAttr.addAttribute("id", "");
            redirectAttr.addAttribute("username", user.getUsername());
        }
        return "redirect:user_allocation";
    }
}
